import sys
import json
from PyQt5 import QtCore, QtGui, QtWidgets
from file_config import pri_key_or_psd
import file_config 
import importlib
from tools.aes_enc import encrypt_message_dict
from file_tools import save_decrypted_data
import json
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(0, 0, 801, 431))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setReadOnly(True)
        
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(40, 440, 131, 61))
        self.pushButton.setObjectName("pushButton")
        
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(250, 440, 131, 61))
        self.pushButton_2.setObjectName("pushButton_2")
        
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(440, 440, 131, 61))
        self.pushButton_3.setObjectName("pushButton_3")
        
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(630, 440, 131, 61))
        self.pushButton_4.setObjectName("pushButton_4")
        
        self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_5.setGeometry(QtCore.QRect(70, 520, 661, 41))
        self.pushButton_5.setObjectName("pushButton_5")
        
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.pushButton.clicked.connect(lambda: self.show_input_dialog("bank_card"))
        self.pushButton_2.clicked.connect(lambda: self.show_input_dialog("email"))
        self.pushButton_3.clicked.connect(lambda: self.show_input_dialog("personal_info"))
        self.pushButton_4.clicked.connect(lambda: self.show_input_dialog("website"))
        self.pushButton_5.clicked.connect(self.show_query_dialog)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "增加银行卡信息"))
        self.pushButton_2.setText(_translate("MainWindow", "增加邮箱信息"))
        self.pushButton_3.setText(_translate("MainWindow", "增加个人信息"))
        self.pushButton_4.setText(_translate("MainWindow", "增加网址信息"))
        self.pushButton_5.setText(_translate("MainWindow", "信息查询"))

    def show_data_dialog(self):
            dialog = QtWidgets.QDialog()
            layout = QtWidgets.QVBoxLayout(dialog)
    
            btn_bank = QtWidgets.QPushButton("银行卡信息", dialog)
            btn_email = QtWidgets.QPushButton("邮箱信息", dialog)
            btn_personal = QtWidgets.QPushButton("个人信息", dialog)
            btn_website = QtWidgets.QPushButton("网址信息", dialog)
    
            layout.addWidget(btn_bank)
            layout.addWidget(btn_email)
            layout.addWidget(btn_personal)
            layout.addWidget(btn_website)
    
            btn_bank.clicked.connect(lambda: self.show_data_in_line_edit(dialog, "bank_card"))
            btn_email.clicked.connect(lambda: self.show_data_in_line_edit(dialog, "email"))
            btn_personal.clicked.connect(lambda: self.show_data_in_line_edit(dialog, "personal_info"))
            btn_website.clicked.connect(lambda: self.show_data_in_line_edit(dialog, "website"))
    
            dialog.exec_()
    
    def show_input_dialog(self, info_type):
        dialog = QtWidgets.QDialog()
        layout = QtWidgets.QFormLayout(dialog)

        if info_type == "bank_card":
            bank_card_number = QtWidgets.QLineEdit(dialog)
            bank_card_password = QtWidgets.QLineEdit(dialog)
            user_name = QtWidgets.QLineEdit(dialog)
            user_id_number = QtWidgets.QLineEdit(dialog)
            layout.addRow("银行卡账号:", bank_card_number)
            layout.addRow("银行卡密码:", bank_card_password)
            layout.addRow("用户名字:", user_name)
            layout.addRow("用户身份证号码:", user_id_number)

            def save_data():
                data = {
                    "type": "aes",
                    "file_type": "bank_card",
                    "bank_card_number": bank_card_number.text(),
                    "bank_card_password": bank_card_password.text(),
                    "user_name": user_name.text(),
                    "user_id_number": user_id_number.text()
                }
                self.save_json_data(data)
                dialog.accept()

        elif info_type == "email":
            email_account = QtWidgets.QLineEdit(dialog)
            email_password = QtWidgets.QLineEdit(dialog)
            email_website = QtWidgets.QLineEdit(dialog)
            email_applicant = QtWidgets.QLineEdit(dialog)
            layout.addRow("邮箱账号:", email_account)
            layout.addRow("邮箱密码:", email_password)
            layout.addRow("邮箱所登录的网站:", email_website)
            layout.addRow("邮箱的申请者:", email_applicant)

            def save_data():
                data = {
                    "type": "aes",
                    "file_type": "email",
                    "email_account": email_account.text(),
                    "email_password": email_password.text(),
                    "email_website": email_website.text(),
                    "email_applicant": email_applicant.text()
                }
                self.save_json_data(data)
                dialog.accept()

        elif info_type == "personal_info":
            user_name = QtWidgets.QLineEdit(dialog)
            user_id_number = QtWidgets.QLineEdit(dialog)
            user_address = QtWidgets.QLineEdit(dialog)
            user_phone = QtWidgets.QLineEdit(dialog)
            user_email = QtWidgets.QLineEdit(dialog)
            layout.addRow("用户名字:", user_name)
            layout.addRow("用户的身份证号码:", user_id_number)
            layout.addRow("用户的家庭住址:", user_address)
            layout.addRow("用户的手机号:", user_phone)
            layout.addRow("用户的邮箱:", user_email)

            def save_data():
                data = {
                    "type": "rsa",
                    "file_type": "personal_info",
                    "user_name": user_name.text(),
                    "user_id_number": user_id_number.text(),
                    "user_address": user_address.text(),
                    "user_phone": user_phone.text(),
                    "user_email": user_email.text()
                }
                self.save_json_data(data)
                dialog.accept()

        elif info_type == "website":
            website_url = QtWidgets.QLineEdit(dialog)
            website_keyword = QtWidgets.QLineEdit(dialog)
            website_account = QtWidgets.QLineEdit(dialog)
            website_password = QtWidgets.QLineEdit(dialog)
            layout.addRow("网站的url:", website_url)
            layout.addRow("网站的关键词:", website_keyword)
            layout.addRow("网站的账号:", website_account)
            layout.addRow("网站的密码:", website_password)

            def save_data():
                data = {
                    "type": "aes",
                    "file_type": "website",
                    "website_url": website_url.text(),
                    "website_keyword": website_keyword.text(),
                    "website_account": website_account.text(),
                    "website_password": website_password.text()
                }
                self.save_json_data(data)
                dialog.accept()

        save_button = QtWidgets.QPushButton("保存", dialog)
        save_button.clicked.connect(save_data)
        layout.addWidget(save_button)
        dialog.exec_()
    def save_json_data(self, data):
        with open('data.json', 'a', encoding='utf-8') as file:
            json.dump(data, file, ensure_ascii=False)
            file.write('\n')

    def show_query_dialog(self):
        dialog = QtWidgets.QDialog()
        dialog.setWindowTitle("查询信息")

        layout = QtWidgets.QVBoxLayout(dialog)

        self.query_buttons = []
        for data_type in ["bank_card", "email", "personal_info", "website"]:
            button = QtWidgets.QPushButton(data_type)
            button.clicked.connect(lambda _, dt=data_type: self.show_data_in_line_edit(dialog, dt))
            self.query_buttons.append(button)
            layout.addWidget(button, alignment=QtCore.Qt.AlignHCenter)
        
        all_data_button = QtWidgets.QPushButton("显示所有数据")
        all_data_button.clicked.connect(lambda: self.show_data_in_line_edit(dialog))
        layout.addWidget(all_data_button, alignment=QtCore.Qt.AlignHCenter)
        
        dialog.setLayout(layout)
        dialog.exec_()

    def show_data_in_line_edit(self, dialog, data_type=None):
        translation = {
            "bank_card_number": "银行卡账号",
            "bank_card_password": "银行卡密码",
            "user_name": "用户名",
            "user_id_number": "用户身份证号码",
            "email_account": "邮箱账号",
            "email_password": "邮箱密码",
            "email_website": "邮箱所登录的网站",
            "email_applicant": "邮箱的申请者",
            "user_address": "用户家庭住址",
            "user_phone": "用户手机号",
            "user_email": "用户邮箱",
            "website_url": "网站URL",
            "website_keywords": "网站关键词",
            "website_account": "网站账号",
            "website_password": "网站密码"
        }

        try:
            data_list = []
            with open('C:\\Users\\29741\\Desktop\\test\\file\\user1\\code1.py', 'r', encoding='utf-8') as file:
                for line in file:
                    try:
                        data = eval(line.strip())  # 使用 eval 将每行字符串转化为字典
                        data_list.append(data)
                    except (SyntaxError, NameError):
                        self.lineEdit.setText("数据文件格式错误。")
                        return

            if data_type:
                filtered_data = [data for data in data_list if data.get("file_type") == data_type]
            else:
                filtered_data = data_list

            display_texts = []
            for data in filtered_data:
                translated_data = {translation.get(k, k): v for k, v in data.items()}
                display_texts.append(self.format_data(translated_data))

            display_text = "\n\n".join(display_texts)
            self.lineEdit.setText(display_text)

        except FileNotFoundError:
            self.lineEdit.setText("数据文件未找到。")
        except json.JSONDecodeError:
            self.lineEdit.setText("数据文件格式错误。")
        dialog.accept()

    def format_data(self, data):
        max_width = self.lineEdit.width() // 8  # 估计字符宽度
        formatted_lines = []
        for key, value in data.items():
            line = f"{key}: {value}"
            formatted_lines.append(line.center(max_width))
        return "\n".join(formatted_lines)

    
    def show_bank_card_dialog(self):
        dialog = QtWidgets.QDialog()
        dialog.setWindowTitle("增加银行卡信息")

        layout = QtWidgets.QVBoxLayout()

        layout.addWidget(QtWidgets.QLabel("银行卡账号:"))
        bank_card_number = QtWidgets.QLineEdit()
        layout.addWidget(bank_card_number)

        layout.addWidget(QtWidgets.QLabel("银行卡密码:"))
        bank_card_password = QtWidgets.QLineEdit()
        layout.addWidget(bank_card_password)

        layout.addWidget(QtWidgets.QLabel("用户名字:"))
        user_name = QtWidgets.QLineEdit()
        layout.addWidget(user_name)

        layout.addWidget(QtWidgets.QLabel("用户身份证号码:"))
        user_id_number = QtWidgets.QLineEdit()
        layout.addWidget(user_id_number)

        save_button = QtWidgets.QPushButton("保存")
        save_button.clicked.connect(lambda: self.save_data_to_json(dialog, {
            "type":"aes",
            "file_type": "bank_card",
            "bank_card_number": bank_card_number.text(),
            "bank_card_password": bank_card_password.text(),
            "user_name": user_name.text(),
            "user_id_number": user_id_number.text()
        }))
        layout.addWidget(save_button)

        dialog.setLayout(layout)
        dialog.exec_()

    def show_email_dialog(self):
        dialog = QtWidgets.QDialog()
        dialog.setWindowTitle("增加邮箱信息")

        layout = QtWidgets.QVBoxLayout()

        layout.addWidget(QtWidgets.QLabel("邮箱账号:"))
        email_account = QtWidgets.QLineEdit()
        layout.addWidget(email_account)

        layout.addWidget(QtWidgets.QLabel("邮箱密码:"))
        email_password = QtWidgets.QLineEdit()
        layout.addWidget(email_password)

        layout.addWidget(QtWidgets.QLabel("邮箱所登录的网站:"))
        email_website = QtWidgets.QLineEdit()
        layout.addWidget(email_website)

        layout.addWidget(QtWidgets.QLabel("邮箱的申请者:"))
        email_applicant = QtWidgets.QLineEdit()
        layout.addWidget(email_applicant)

        save_button = QtWidgets.QPushButton("保存")
        save_button.clicked.connect(lambda: self.save_data_to_json(dialog, {
            "type":"aes",
            "file_type": "email",
            "email_account": email_account.text(),
            "email_password": email_password.text(),
            "email_website": email_website.text(),
            "email_applicant": email_applicant.text()
        }))
        layout.addWidget(save_button)

        dialog.setLayout(layout)
        dialog.exec_()

    def show_personal_info_dialog(self):
        dialog = QtWidgets.QDialog()
        dialog.setWindowTitle("增加个人信息")

        layout = QtWidgets.QVBoxLayout()

        layout.addWidget(QtWidgets.QLabel("用户名字:"))
        user_name = QtWidgets.QLineEdit()
        layout.addWidget(user_name)

        layout.addWidget(QtWidgets.QLabel("用户的身份证号码:"))
        user_id_number = QtWidgets.QLineEdit()
        layout.addWidget(user_id_number)

        layout.addWidget(QtWidgets.QLabel("用户的家庭住址:"))
        user_address = QtWidgets.QLineEdit()
        layout.addWidget(user_address)

        layout.addWidget(QtWidgets.QLabel("用户的手机号:"))
        user_phone = QtWidgets.QLineEdit()
        layout.addWidget(user_phone)

        layout.addWidget(QtWidgets.QLabel("用户的邮箱:"))
        user_email = QtWidgets.QLineEdit()
        layout.addWidget(user_email)

        save_button = QtWidgets.QPushButton("保存")
        save_button.clicked.connect(lambda: self.save_data_to_json(dialog, {
            "type":"rsa",
            "file_type": "personal_info",
            "user_name": user_name.text(),
            "user_id_number": user_id_number.text(),
            "user_address": user_address.text(),
            "user_phone": user_phone.text(),
            "user_email": user_email.text()
        }))
        layout.addWidget(save_button)

        dialog.setLayout(layout)
        dialog.exec_()

    def show_website_info_dialog(self):
        dialog = QtWidgets.QDialog()
        dialog.setWindowTitle("增加网址信息")

        layout = QtWidgets.QVBoxLayout()

        layout.addWidget(QtWidgets.QLabel("网站的URL:"))
        website_url = QtWidgets.QLineEdit()
        layout.addWidget(website_url)

        layout.addWidget(QtWidgets.QLabel("网站的关键词:"))
        website_keywords = QtWidgets.QLineEdit()
        layout.addWidget(website_keywords)

        layout.addWidget(QtWidgets.QLabel("网站的账号:"))
        website_account = QtWidgets.QLineEdit()
        layout.addWidget(website_account)

        layout.addWidget(QtWidgets.QLabel("网站的密码:"))
        website_password = QtWidgets.QLineEdit()
        layout.addWidget(website_password)

        save_button = QtWidgets.QPushButton("保存")
        save_button.clicked.connect(lambda: self.save_data_to_json(dialog, {
            "type":"rsa",
            "file_type": "website",
            "website_url": website_url.text(),
            "website_keywords": website_keywords.text(),
            "website_account": website_account.text(),
            "website_password": website_password.text()
        }))
        layout.addWidget(save_button)

        dialog.setLayout(layout)
        dialog.exec_()

    def show_data_query_dialog(self):
        dialog = QtWidgets.QDialog()
        dialog.setWindowTitle("信息查询")

        layout = QtWidgets.QVBoxLayout()

        button_bank_card = QtWidgets.QPushButton("银行卡信息")
        button_bank_card.clicked.connect(lambda: self.show_data_in_line_edit(dialog, "bank_card"))
        layout.addWidget(button_bank_card)

        button_email = QtWidgets.QPushButton("邮箱信息")
        button_email.clicked.connect(lambda: self.show_data_in_line_edit(dialog, "email"))
        layout.addWidget(button_email)

        button_personal_info = QtWidgets.QPushButton("个人信息")
        button_personal_info.clicked.connect(lambda: self.show_data_in_line_edit(dialog, "personal_info"))
        layout.addWidget(button_personal_info)

        button_website = QtWidgets.QPushButton("网址信息")
        button_website.clicked.connect(lambda: self.show_data_in_line_edit(dialog, "website"))
        layout.addWidget(button_website)

        dialog.setLayout(layout)
        dialog.exec_()

    def save_data_to_json(self, dialog, data):
        importlib.reload(file_config)
        pri_key_or_psd = file_config.pri_key_or_psd
        encrypted_message, encrypt_type, pri_key_or_psd = encrypt_message_dict(data, pri_key_or_psd,user_nums=1)
        save_decrypted_data(1)
    import json

    def show_data_in_line_edit(self, dialog, data_type=None):
        try:
            data_list = []
            with open('C:\\Users\\29741\\Desktop\\test\\file\\user1\\code1.py', 'r', encoding='utf-8') as file:
                for line in file:
                    try:
                        data = eval(line.strip())  # 使用 eval 将每行字符串转化为字典
                        data_list.append(data)
                    except (SyntaxError, NameError):
                        self.lineEdit.setText("数据文件格式错误。")
                        return
    
            if data_type:
                filtered_data = [data for data in data_list if data.get("file_type") == data_type]
            else:
                filtered_data = data_list
    
            display_text = json.dumps(filtered_data, ensure_ascii=False, indent=4)
            self.lineEdit.setText(display_text)
    
        except FileNotFoundError:
            self.lineEdit.setText("数据文件未找到。")
        except json.JSONDecodeError:
            self.lineEdit.setText("数据文件格式错误。")
        dialog.accept()



if __name__ == "__main__":
    last_user = 1
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.show_data_in_line_edit
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
